package com.base.cn.platform.os.controller.op.article;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.service.manage.op.article.OpArticleService;
import com.base.cn.platform.os.service.manage.op.article.OpArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资讯管理
 * @author zsw
 * @since 2018/5/4
 */
@Controller
@RequestMapping("/admin/op/article")
public class AdminOpArticleController extends AdminBaseController {

    @Autowired
    private OpArticleService opArticleService;
    @Autowired
    private OpArticleTypeService opArticleTypeService;

    /**
     * 搜索资讯列表
     * @param model
     * @return 资讯列表页面
     */
    @RequestMapping("/queryArticleList")
    public String queryArticleList(Model model){
        List<Map<String, Object>> articleTypeList = opArticleTypeService.queryAllArticleType(new HashMap<>());
        model.addAttribute("articleTypeList",articleTypeList);
        return resultPage("/op/article/article-list");
    }

    /**
     * ajax搜索资讯列表
     * @param model
     * @param currentPage 当前页码
     * @param params 查询条件
     * @return 资讯列表页面
     */
    @RequestMapping("/queryAjaxArticleList")
    public String queryAjaxArticleList(Model model,
                                       @RequestParam(value = "currentPage", required = false,defaultValue = "1")int currentPage,
                                       @RequestBody Map<String, Object> params){
        params.put("order",2);
        Map<String, Object> dataMap = opArticleService.queryArticleList(currentPage, params);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/op/article/article-ajax-list");
    }

    /**
     * 跳转资讯添加,修改页面
     * @param model
     * @param articleId 资讯id
     * @return 资讯添加,修改页面
     */
    @RequestMapping(value = {"/toUpdateArticle","/toSaveArticle"})
    public String toSaveUpdateArticle(Model model,
                                      @RequestParam(value = "articleId", required = false, defaultValue = "0")BigDecimal articleId){
        Map<String, Object> article = null;
        if(articleId.compareTo(new BigDecimal(0)) > 0){
            article = opArticleService.findArticleById(articleId, false, 0);
        }
        if(article == null){
            article = new HashMap<>();
        }
        Map<String,Object> opArticleTypeMap = new HashMap<>();
        opArticleTypeMap.put("status",1);
        List<Map<String, Object>> articleTypeList = opArticleTypeService.queryAllArticleType(opArticleTypeMap);
        model.addAttribute("articleTypeList",articleTypeList);
        model.addAttribute("article", article);
        return resultPage("/op/article/article-save-update");
    }

    /**
     * 添加资讯
     * @param article 资讯实体
     * @return
     */
    @RequestMapping("/saveUpdateArticle")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "添加或修改资讯")
    public Map<String,Object> saveUpdateArticle(@RequestBody Map<String,Object> article){
        Object context = article.remove("context");
        Map<String, Object> articleContext = Collections.singletonMap("context", context);
        article.put("articleContext", articleContext);
        return opArticleService.saveUpdateArticle(article);
    }

    /**
     * 资讯批量发布
     * @param articleIds 资讯id串
     * @return
     */
    @RequestMapping("/batchPublishArticle")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "资讯发布")
    public Map<String, Object> batchPublishArticle(@RequestParam("articleIds") String articleIds) {
        return opArticleService.batchPublishArticle(articleIds);
    }

    /**
     * 根据资讯id批量操作 修改，恢复
     * @param articleIds 资讯id串
     * @param status 0正常 1冻结 2删除
     * @return
     */
    @RequestMapping("/updateArticleStatusByIds")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改资状态")
    public Map<String,Object> updateArticleStatusByIds(@RequestParam("articleIds")String articleIds,
                                                       @RequestParam("status")Integer status){
        return opArticleService.updateArticleStatusByIds(articleIds,status);
    }


    //=========================================其他模块调用资讯方法=========================================================
    /**
     * 搜索列表
     * @param model
     * @param selectType 选择资讯list
     * @return 资讯选择列表
     */
    @RequestMapping("/selectArticleList")
    public String selectArticleList(Model model,
                                    @RequestParam(value = "selectType",required = false,defaultValue = "radio") String selectType){
        model.addAttribute("selectType",selectType);
        return resultPage("/op/article/select_article_list");
    }

    /**
     * 获取选择资讯数据
     * @param params
     * @param currentPage 当前页码
     * @return 列表页面
     */
    @RequestMapping("/ajaxSelectArticleList")
    public String ajaxSelectArticleList(Model model,
                                     @RequestBody Map<String,Object> params,
                                     @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage){
        params.put("status",0);//只查询状态正常的
        params.put("releaseStatus",2);//只查询已发布的资讯
        Map<String,Object> dataMap = opArticleService.queryArticleList(currentPage,params);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("selectType",params.get("selectType"));
        return resultPage("/op/article/select_article_ajax_list");
    }
    //==================================================================================================================

}
